package com.customization.huangwm.cronJob.executeActions.SW007;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.customization.huangwm.utils.DateTools;
import com.engine.common.util.ServiceUtil;
import com.engine.workflow.entity.publicApi.PAResponseEntity;
import com.engine.workflow.entity.publicApi.ReqOperateRequestEntity;
import com.engine.workflow.publicApi.WorkflowRequestOperatePA;
import com.engine.workflow.publicApi.impl.WorkflowRequestOperatePAImpl;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.GCONST;
import weaver.hrm.User;
import weaver.system.SysRemindWorkflow;
import weaver.workflow.webservices.WorkflowRequestTableField;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2023-05-28
 * @Version 1.0
 * @Description TODO
 **/
public class test {
    public static void main(String[] args) throws Exception {
        String rootPath = "E:\\WEAVER9\\ecology\\";
        GCONST.setRootPath(rootPath);
        GCONST.setServerName("ecology");


        String sql = "select * from uf_007_SchedulerV1 ";
//        RecordSet rs = new RecordSet();
//        RecordSet rs2 = new RecordSet();
//        rs.execute(sql);
//        while (rs.next()){
//            String remidtype = rs.getString("remidtype");//0-天 1-周 2-月
//            String remidtime = rs.getString("remidtime");//提醒周期 在remidtype基础上增加对应的时间
//            String lastremiddate = rs.getString("lastremiddate");//上一次提醒日期
//            String nextreminddate = rs.getString("nextreminddate");//下一次提醒日期
//            String billid = rs.getString("id");
//            String code = rs.getString("code");//编号
//            String remidtag = rs.getString("remidtag");//提醒方式 A(只提醒)　B(帶工單模板内容提醒)　C(指定提醒流程)
//
//            String creator = rs.getString("creator");
//            String remiduser = rs.getString("remiduser");
//            String dep = rs.getString("dep");
//            String content1 = rs.getString("content1");
//
//            Map<String,String> maindata = new HashMap<>();
//            maindata.put("scheduleid",billid);
//            maindata.put("remiduser",remiduser);
//            maindata.put("creator",creator);
//            maindata.put("dep",dep);
//            maindata.put("content1",content1);
//
//            System.err.println("billid="+billid+"  remindtag="+remidtag);
//
//            if(remidtag.equals("0")){
//                System.err.println("情况1发起:");
//                SysRemindWorkflow remind = new SysRemindWorkflow();
//                remind.make("Work Scheduler Remind V1",
//                        0, 0, 0, 0, 1,
//                        "1",
//                        "<span> 您當前有工單計劃需要執行<a href='/spa/cube/index.html#/main/cube/card?type=0&modeId=204&formId=-430&opentype=0&customid=207&viewfrom=fromsearchlist&guid=card&billid="+billid+"' target='_blank' >計劃編號:"+code+"</a> </span>", 0);
//
//            }else if(remidtag.equals("1")){
//                //固定模板提醒
//                System.err.println("情况2发起:");
//                String requestid = remind(maindata);
//                System.err.println("提醒流程requestid:"+requestid);
//            }else if(remidtag.equals("2")){
//                //觸發指定工作流程
//                System.err.println("情况3发起:");
//                remind2(rs.getString("remindReq"),rs2,remiduser);
//            }
//        }

        DateTools dateTools = new DateTools();
        System.err.println(dateTools.getWeekOfDate());
        System.err.println(dateTools.getLastWeekDayOfMonth());
        System.err.println(dateTools.getFirstWeekDayOfMonth());
    }

    private static String remind(Map<String,String> maindata){

        ReqOperateRequestEntity requestParam = new ReqOperateRequestEntity();
        //流程流转扩展参数
        Map<String,Object> otherParams = new HashMap<>();
        otherParams.put("isVerifyPer","0"); //不验证用户创建流程权限
        otherParams.put("isnextflow","1");

        requestParam.setMainData(getFormMainData(maindata));
        //requestParam.setDetailData(getFormDetailData(detaildata,table03));
        WorkflowVersion version = new WorkflowVersion();
        int activeid = Integer.parseInt(version.getActiveVersionWFID("224"));
        requestParam.setWorkflowId(activeid);
        requestParam.setRequestName("工單提醒");
        requestParam.setOtherParams(otherParams);

        WorkflowRequestOperatePA operatePA = ServiceUtil.getService(WorkflowRequestOperatePAImpl.class);
        User user = new User(1);
        PAResponseEntity respose = operatePA.doCreateRequest(user,requestParam);

        if(respose.getCode().getStatusCode()>1){
            System.err.println(JSONObject.toJSONString(respose));
            throw  new RuntimeException("自动发起情况2失败");
        }else{
            String obj = JSONObject.toJSONString(respose.getData());
            JSONObject json = JSONObject.parseObject(obj);
            return json.getString("requestid");
        }

    }

    private static List<WorkflowRequestTableField> getFormMainData(Map<String,String> maindata) {
        List<WorkflowRequestTableField> mainFields = new ArrayList<>();

        //code
        WorkflowRequestTableField applicationUser = new WorkflowRequestTableField();
        applicationUser.setFieldName("scheduleid");
        applicationUser.setFieldValue(maindata.get("scheduleid"));
        applicationUser.setView(true);
        applicationUser.setEdit(true);
        mainFields.add(applicationUser);
        //remiduser
        WorkflowRequestTableField remiduser = new WorkflowRequestTableField();
        remiduser.setFieldName("remiduser");
        remiduser.setFieldValue(maindata.get("remiduser"));
        remiduser.setView(true);
        remiduser.setEdit(true);
        mainFields.add(remiduser);
        //creator
        WorkflowRequestTableField creator = new WorkflowRequestTableField();
        creator.setFieldName("creator");
        creator.setFieldValue(maindata.get("creator"));
        creator.setView(true);
        creator.setEdit(true);
        mainFields.add(creator);
        //dep
        WorkflowRequestTableField dep = new WorkflowRequestTableField();
        dep.setFieldName("dep");
        dep.setFieldValue(maindata.get("dep"));
        dep.setView(true);
        dep.setEdit(true);
        mainFields.add(dep);
        //content1
        WorkflowRequestTableField content1 = new WorkflowRequestTableField();
        content1.setFieldName("content1");
        content1.setFieldValue(maindata.get("content1"));
        content1.setView(true);
        content1.setEdit(true);
        mainFields.add(content1);

        return mainFields;
    }

    private static void remind2(String wfid,RecordSet rs,String remindUser){
        ReqOperateRequestEntity requestParam = new ReqOperateRequestEntity();
        //流程流转扩展参数
        Map<String,Object> otherParams = new HashMap<>();
        otherParams.put("isVerifyPer","0"); //不验证用户创建流程权限
        //otherParams.put("isnextflow","1");

        WorkflowVersion version = new WorkflowVersion();
        int activeid = Integer.parseInt(version.getActiveVersionWFID(wfid));

        rs.execute("select * from workflow_base where id="+activeid);
        rs.next();

        //requestParam.setMainData(getFormMainData(maindata));
        //requestParam.setDetailData(getFormDetailData(detaildata,table03));
        requestParam.setWorkflowId(activeid);
        requestParam.setRequestName( rs.getString("workflowname")+"(工單提醒)");
        requestParam.setOtherParams(otherParams);

        String[] l = remindUser.split(",");
        for(String s : l){
            WorkflowRequestOperatePA operatePA = ServiceUtil.getService(WorkflowRequestOperatePAImpl.class);
            User user = new User(Integer.parseInt(s));
            PAResponseEntity respose = operatePA.doCreateRequest(user,requestParam);
            if(respose.getCode().getStatusCode()>1){
                System.err.println(JSONObject.toJSONString(respose));
                throw  new RuntimeException("自动发起情况3失败");
            }else{
                String obj = JSONObject.toJSONString(respose.getData());
                System.err.println("自动发起情况3流程");
                System.err.println(obj);
            }
        }
    }
}
